Xmpp.prototype.onStateChange = function(state)
{
	if (state > 0)
	{
	}
}


Xmpp.prototype.addUser =  function(jid, name, group)
{
	var roster = document.getElementById("roster-root");
	var grp_root =  roster;
	var usr_item = null;
	if (group)
		grp_root = roster.getElementById(group);
	usr_item = grp_root.getElementById("jid");
	if (!usr_item)
		usr_item = document.createElement("treeitem");
}

Xmpp.prototype.onVCard = function()
{
	var tree=document.getElementById("roster-tree");
	var jid = tree.view.getCellText(tree.currentIndex,"jid");
	this.getVCard(jid);
}

Xmpp.prototype.onChatTo = function()
{
	var tree=document.getElementById("results-tree");
	var to = tree.view.getCellText(tree.currentIndex,"jid");
	var browser = CClasses["@mozilla.org/embedcomp/window-watcher;1"].getService(CI.nsIWindowWatcher);
	var win = browser.getWindowByName(to, browser.activeWindow );
	if (!win)
	{
		win = browser.openWindow(null, "chrome://xmpp/content/chat.xul", to, "chrome", null);
		document.title = to;
		win.name = to;
	}
}

Xmpp.prototype.onSearch = function()
{
	var directory = document.getElementById("xmpp.search.directory.menu");
	var first = document.getElementById("xmpp.search.first");
	var last =  document.getElementById("xmpp.search.last");
	var nick =  document.getElementById("xmpp.search.nick");
	var email =  document.getElementById("xmpp.search.email");
	this.search(directory.value, first.value, last.value, nick.value, email.value);
}

Xmpp.prototype.onButtonClick = function (event)
{
	switch (event.button)
	{
	case 0:
		break;
	case 2:
		var tree=document.getElementById("roster-tree");
	    var selection=tree.view.selection;
	    var box=tree.boxObject;
		var p = new Object();
		if (selection.count)
		{	
			var row = {}, col = {}, part = {};
			box.getCellAt(event.clientX, event.clientY, row, col, part);
			this.showContextMenu(event.clientX, event.clientY, "search-context-menu", "results-tree");
		}
		break;
	};
}

var jRL = null;
var ds = null;

function onLoad()
{
	var i = 0;
	var tree = document.getElementById("results-tree");
	ds  = jXmpp.getDataSource();
	var rdf = CClasses["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
	var rdfCU = CClasses["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils); 
	var search = rdf.GetResource("jabber:iq:search");
	var ctn = rdfCU.MakeSeq(ds, search);
	if(tree)
	{
		window.removeEventListener("onLoad", onLoad, false);
		ds  = jXmpp.getDataSource();
		tree.database.AddDataSource(ds);
		tree.builder.rebuild();
		jRL = new TreeListener(tree);
		ds.AddObserver(jRL);
	}
	var dir = document.getElementById("xmpp.search.directory");
	if(dir)
	{
		var iter = ctn.GetElements();
		while (iter.hasMoreElements())
		{
			var elem = iter.getNext().QueryInterface(CI.nsIRDFResource);
			var name = getRDFLiteral(ds, elem, rdf.GetResource("name"));
			var menuitem = document.createElement("menuitem");
			menuitem.setAttribute("id", "xmpp.search.directory."+i);
			menuitem.setAttribute("label", name);
			menuitem.setAttribute("value", elem.Value);
			dir.appendChild(menuitem);
			i++;
		}
		search = rdf.GetResource("urn:xmpp:features");
		ctn = rdfCU.MakeSeq(ds, search);
		iter = ctn.GetElements();
	}
}

function onUnload()
{
	ds.RemoveObserver(jRL);
	jRL = null;
}

window.addEventListener("load", onLoad, false);
window.addEventListener("unload", onUnload, false);
